﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace PictureSplit
{
	// Token: 0x02000006 RID: 6
	public partial class Main : Form
	{
		// Token: 0x0600001A RID: 26 RVA: 0x00002B58 File Offset: 0x00001B58
		public Main()
		{
			this.InitializeComponent();
		}

		// Token: 0x0600001B RID: 27 RVA: 0x00002C44 File Offset: 0x00001C44
		private void ControlEnable(Control control, bool enable)
		{
			if (control.InvokeRequired)
			{
				Main.ControlEnableDelegate method = new Main.ControlEnableDelegate(this.ControlEnable);
				base.Invoke(method, new object[]
				{
					control,
					enable
				});
			}
			else
			{
				control.Enabled = enable;
			}
		}

		// Token: 0x0600001C RID: 28 RVA: 0x00002C98 File Offset: 0x00001C98
		private void ScreenCursor(Cursor cursor)
		{
			if (base.InvokeRequired)
			{
				Main.screenCursorDelegate method = new Main.screenCursorDelegate(this.ScreenCursor);
				base.Invoke(method, new object[]
				{
					cursor
				});
			}
			else
			{
				this.Cursor = cursor;
			}
		}

		// Token: 0x0600001D RID: 29 RVA: 0x00002CE4 File Offset: 0x00001CE4
		private void writeOutPut(string output)
		{
			if (this.txtFileInfo.InvokeRequired)
			{
				Main.WriteOutputDelegate method = new Main.WriteOutputDelegate(this.writeOutPut);
				base.Invoke(method, new object[]
				{
					output
				});
			}
			else
			{
				this.txtFileInfo.Text = output;
			}
		}

		// Token: 0x0600001E RID: 30 RVA: 0x00002D38 File Offset: 0x00001D38
		private void OpenFile()
		{
			int num = 101;
			int num2 = 92;
			this.ScreenCursor(Cursors.WaitCursor);
			this.ControlEnable(this.grpOperate, false);
			this.ControlEnable(this.groupBox1, false);
			this.openFileDialog.FileName = "";
			this.openFileDialog.Filter = "JPEG (*.JPG;*.JPEG;*.JPE;*.JFIF)|*.JPG;*.JPEG;*.JPE;*.JFIF|位图文件 (*.bmp)|*.bmp|GIF (*.GIF)|*.GIF|TIFF (*.TIF;*.TIFF)|*.TIF;*.TIFF|PNG (*.PNG)|*.PNG|所有文件 (*.*)|*.*";
			this.openFileDialog.FilterIndex = 1;
			this.openFileDialog.RestoreDirectory = true;
			if (this.openFileDialog.ShowDialog() == DialogResult.OK)
			{
				this.strSourcePictureName = this.openFileDialog.FileName;
				FileInfoClass fileInfoClass = new FileInfoClass(this.strSourcePictureName);
				FileInfo fileInfo = new FileInfo(this.strSourcePictureName);
                m_imageSrc = new Bitmap(this.strSourcePictureName);
				this.intSourcePictureWidth = m_imageSrc.Size.Width;
				this.intSourcePictureHeight = m_imageSrc.Size.Height;
				string output = new StringBuilder().AppendLine("文件名:\t\t" + this.strSourcePictureName).AppendLine("尺寸:\t\t" + m_imageSrc.Size.Width.ToString() + " x " + m_imageSrc.Size.Height.ToString()).AppendLine("大小:\t\t" + fileInfoClass.FileSize.ToString() + " bytes").AppendLine("创建日期:\t" + fileInfo.CreationTime.ToShortDateString()).AppendLine("修改日期:\t" + fileInfo.LastWriteTime.ToShortDateString()).ToString();
				this.writeOutPut(output);
				Console.WriteLine(m_imageSrc.Size.ToString());
				int num3 = num;
				int num4 = num2;
				int x = 0;
				int y = 0;
				int num5 = m_imageSrc.Width;
				int num6 = m_imageSrc.Height;
				string text = "HW";
				string text2 = text;
				if (text2 != null)
				{
					if (!(text2 == "HW"))
					{
						if (!(text2 == "W"))
						{
							if (!(text2 == "H"))
							{
								if (text2 == "Cut")
								{
									if ((double)m_imageSrc.Width / (double)m_imageSrc.Height > (double)num3 / (double)num4)
									{
										num6 = m_imageSrc.Height;
										num5 = m_imageSrc.Height * num3 / num4;
										y = 0;
										x = (m_imageSrc.Width - num5) / 2;
									}
									else
									{
										num5 = m_imageSrc.Width;
										num6 = m_imageSrc.Width * num2 / num3;
										x = 0;
										y = (m_imageSrc.Height - num6) / 2;
									}
								}
							}
							else
							{
								num3 = m_imageSrc.Width * num2 / m_imageSrc.Height;
							}
						}
						else
						{
							num4 = m_imageSrc.Height * num / m_imageSrc.Width;
						}
					}
				}
				Image image2 = new Bitmap(num3, num4);
				Graphics graphics = Graphics.FromImage(image2);
				graphics.InterpolationMode = InterpolationMode.High;
				graphics.SmoothingMode = SmoothingMode.HighQuality;
				graphics.Clear(Color.Transparent);
				graphics.DrawImage(m_imageSrc, new Rectangle(0, 0, num3, num4), new Rectangle(x, y, num5, num6), GraphicsUnit.Pixel);
				this.previewPictureBox.Image = image2;
				this.ControlEnable(this.grpOperate, true);
			}
			this.ControlEnable(this.groupBox1, true);
			this.ScreenCursor(Cursors.Default);
		}

		// Token: 0x0600001F RID: 31 RVA: 0x000030C4 File Offset: 0x000020C4
		private void btnOpenFile_Click(object sender, EventArgs e)
		{
			ThreadStart start = new ThreadStart(this.OpenFile);
			Thread thread = new Thread(start);
			thread.SetApartmentState(ApartmentState.STA);
			thread.Start();
		}

		// Token: 0x06000020 RID: 32 RVA: 0x000030F8 File Offset: 0x000020F8
		private void ProcessRadioButton()
		{
			this.btnSplit.Enabled = true;
			this.txtWidthNumber.Enabled = this.rabNumberSplit.Checked;
			this.txtHeightNumber.Enabled = this.rabNumberSplit.Checked;
			this.txtWidthPixel.Enabled = this.rabSizeSplit.Checked;
			this.txtHeightPixel.Enabled = this.rabSizeSplit.Checked;
		}

		// Token: 0x06000021 RID: 33 RVA: 0x0000316F File Offset: 0x0000216F
		private void rabSizeSplit_Click(object sender, EventArgs e)
		{
			this.ProcessRadioButton();
		}

		// Token: 0x06000022 RID: 34 RVA: 0x00003179 File Offset: 0x00002179
		private void rabNumberSplit_Click(object sender, EventArgs e)
		{
			this.ProcessRadioButton();
		}

		// Token: 0x06000023 RID: 35 RVA: 0x00003184 File Offset: 0x00002184
		private void SplitPicture()
        {
            int quality = 0;
            int width_num = 0;
			int height_num = 0;
			int tile_w = 0;
			int tile_h = 0;
			this.groupBox1.Enabled = false;
			this.grpOperate.Enabled = false;
			this.FlashTimer.Enabled = false;
			if (this.rabNumberSplit.Checked)
			{
				if (this.txtWidthNumber.Text != "" & this.txtHeightNumber.Text != "")
				{
					try
					{
						width_num = int.Parse(this.txtWidthNumber.Text);
						height_num = int.Parse(this.txtHeightNumber.Text);
						tile_w = this.intSourcePictureWidth / width_num;
						tile_h = this.intSourcePictureHeight / height_num;
					}
					catch (Exception ex)
					{
						MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Hand);
					}
				}
			}
			else if (this.txtWidthPixel.Text != "" & this.txtHeightPixel.Text != "")
			{
				try
				{
					tile_w = int.Parse(this.txtWidthPixel.Text);
					tile_h = int.Parse(this.txtHeightPixel.Text);
					width_num = (this.intSourcePictureWidth + (tile_w - 1)) / tile_w;
					height_num = (this.intSourcePictureHeight + (tile_h - 1)) / tile_h;
                    quality = int.Parse(this.txtQuality.Text);
                }
				catch (Exception ex2)
				{
					MessageBox.Show(ex2.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Hand);
				}
            }
            string ext = Path.GetExtension(this.strSourcePictureName);
            string path = Path.GetDirectoryName(this.strSourcePictureName);
            string filename = Path.GetFileNameWithoutExtension(this.strSourcePictureName);
            path = path + "\\" + filename + "\\";
            ext = ext.ToLower().Split(new char[]{ '.'})[1];
            Directory.CreateDirectory(path);
            try
            {
                Split_Form split_Form = new Split_Form();
                split_Form.SaveImageFormat = ext;
                split_Form.SourceFileName = this.strSourcePictureName;
                split_Form.WidthNumber = width_num;
                split_Form.HeightNumber = height_num;
                split_Form.WidthPixel = tile_w;
                split_Form.HeightPixel = tile_h;
                split_Form.SaveFileName = path + "a.jpg";
                split_Form.Quality = quality;
                split_Form.ImageSrc = m_imageSrc;
                if (this.chkCreateHtml.Checked)
                {
                    split_Form.IsCreateHtml = true;
                }
                split_Form.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            finally
            {
                this.groupBox1.Enabled = true;
                this.FlashTimer.Enabled = true;
            }
            this.groupBox1.Enabled = true;
			this.grpOperate.Enabled = true;
			this.FlashTimer.Enabled = true;
		}

		// Token: 0x06000024 RID: 36 RVA: 0x000034EC File Offset: 0x000024EC
		private void btnSplit_Click(object sender, EventArgs e)
		{
			this.SplitPicture();
		}

		// Token: 0x06000025 RID: 37 RVA: 0x000034F8 File Offset: 0x000024F8
		private void DrawString(float xLocation, float yLocation, Font font, string Message, int Space)
		{
			for (int i = 0; i < Message.Length; i++)
			{
				if (this.isExit)
				{
					break;
				}
				this.g.DrawString(Message.Substring(i, 1), font, new SolidBrush(this.colorArray[i]), xLocation, yLocation);
				xLocation += (float)Space;
				Application.DoEvents();
			}
		}

		// Token: 0x06000026 RID: 38 RVA: 0x0000356C File Offset: 0x0000256C
		private void timer1_Tick(object sender, EventArgs e)
		{
            return;
			if (this.isExit)
			{
				this.FlashTimer.Enabled = false;
			}
			else
			{
				int num = 25;
				switch (this.step)
				{
				case 0:
					this.green += num;
					if (this.green > 255)
					{
						this.step = 1;
						this.green = 255;
					}
					break;
				case 1:
					this.red -= num;
					if (this.red <= 0)
					{
						this.step = 2;
						this.red = 0;
					}
					break;
				case 2:
					this.blue += num;
					if (this.blue >= 255)
					{
						this.step = 3;
						this.blue = 255;
					}
					break;
				case 3:
					this.green -= num;
					if (this.green <= 0)
					{
						this.step = 4;
						this.green = 0;
					}
					break;
				case 4:
					this.red += num;
					if (this.red > 255)
					{
						this.step = 5;
						this.red = 255;
					}
					break;
				case 5:
					this.blue -= num;
					if (this.blue <= 0)
					{
						this.step = 0;
						this.blue = 0;
					}
					break;
				}
				for (int i = 31; i > 0; i--)
				{
					Color color = this.colorArray[i - 1];
					this.colorArray[i] = color;
				}
				this.colorArray[0] = Color.FromArgb(this.red, this.green, this.blue);
				this.DrawString(60f, 13f, this.font1, "图片分割器", 15);
				this.DrawString(63f, 30f, this.englishFont, this.versionString, 8);
				this.DrawString(63f, 52f, this.smallChineseFont, "感谢您使用本软件，如有任何问题或建议请与我联系。", 11);
				this.DrawString(63f, 63f, this.englishFont, "atomtech@163.com", 10);
				this.DrawString(63f, 76f, this.englishFont, "http://www.game7000.com", 10);
			}
		}

		// Token: 0x06000027 RID: 39 RVA: 0x000037FC File Offset: 0x000027FC
		private void CheckUpdate()
		{
            return;
			Update update = new Update();
			this.versionString += update.GetCurrentVersion();
			if (update.NeedUpdate())
			{
				this.tssMessage.Text = "  发现新程序，点击这里进行升级";
			}
		}

		// Token: 0x06000028 RID: 40 RVA: 0x00003848 File Offset: 0x00002848
		private void Form1_Load(object sender, EventArgs e)
		{
			this.g = this.groupBox2.CreateGraphics();
            for (int i = 0; i < 31; i++)
			{
				this.colorArray[i] = Color.Black;
            }
            return;
            this.DrawString(60f, 13f, this.font1, "图片分割器", 15);
			this.DrawString(63f, 30f, this.englishFont, "PicSplit V1.0", 8);
			this.DrawString(63f, 52f, this.smallChineseFont, "感谢您使用本软件，如有任何问题或建议请与我联系。", 11);
			this.DrawString(63f, 63f, this.englishFont, "atomtech@163.com", 10);
			this.DrawString(63f, 76f, this.englishFont, "http://www.game7000.com", 10);
			this.CheckUpdateThread = new Thread(new ThreadStart(this.CheckUpdate));
			this.CheckUpdateThread.Start();
		}

		// Token: 0x06000029 RID: 41 RVA: 0x00003948 File Offset: 0x00002948
		private void pictureBox1_Click(object sender, EventArgs e)
		{
			Process.Start("http://www.game7000.com");
		}

		// Token: 0x0600002A RID: 42 RVA: 0x00003956 File Offset: 0x00002956
		private void startFlashTimer_Tick(object sender, EventArgs e)
		{
			this.startFlashTimer.Enabled = false;
			this.FlashTimer.Enabled = true;
		}

		// Token: 0x0600002B RID: 43 RVA: 0x00003974 File Offset: 0x00002974
		private void tssMessage_Click(object sender, EventArgs e)
		{
			if (MessageBox.Show("发现更新版本程序，是否需要升级", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
			{
				try
				{
					Process.Start(Application.StartupPath + "\\Update.exe");
					Application.Exit();
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.Message + "\r\n 无法进行升级", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Hand);
				}
			}
		}

		// Token: 0x0600002C RID: 44 RVA: 0x000039F8 File Offset: 0x000029F8
		private void statusFlashTimer_Tick(object sender, EventArgs e)
		{
			if (this.tssMessage.Text != "")
			{
				if (this.tssMessage.ForeColor == Color.Red)
				{
					this.tssMessage.ForeColor = Color.Black;
				}
				else
				{
					this.tssMessage.ForeColor = Color.Red;
				}
			}
		}

		// Token: 0x0400000F RID: 15
		private string strSourcePictureName = "";

		// Token: 0x04000010 RID: 16
		private int intSourcePictureWidth = 0;

		// Token: 0x04000011 RID: 17
		private int intSourcePictureHeight = 0;

		// Token: 0x04000012 RID: 18
		private Thread CheckUpdateThread;

		// Token: 0x04000013 RID: 19
		private string versionString = "PicSplit ";

		// Token: 0x04000014 RID: 20
		private Graphics g;

		// Token: 0x04000015 RID: 21
		private int red = 0;

		// Token: 0x04000016 RID: 22
		private int green = 0;

		// Token: 0x04000017 RID: 23
		private int blue = 0;

		// Token: 0x04000018 RID: 24
		private Random random = new Random();

		// Token: 0x04000019 RID: 25
		private Color[] colorArray = new Color[32];

		// Token: 0x0400001A RID: 26
		private Font font1 = new Font("楷体_GB2312", 12f, FontStyle.Bold);

		// Token: 0x0400001B RID: 27
		private Font englishFont = new Font("Tahoma", 10f, FontStyle.Regular);

		// Token: 0x0400001C RID: 28
		private Font smallChineseFont = new Font("仿宋", 8f, FontStyle.Regular);

		// Token: 0x0400001D RID: 29
		private Label[] lblC1 = new Label[10];

		// Token: 0x0400001E RID: 30
		private Label[] lblC2 = new Label[20];

		// Token: 0x0400001F RID: 31
		private Label[] lblMessageArray = new Label[22];

		// Token: 0x04000020 RID: 32
		private int step = 0;

		// Token: 0x02000007 RID: 7
		// (Invoke) Token: 0x06000030 RID: 48
		private delegate void ControlEnableDelegate(Control control, bool enable);

		// Token: 0x02000008 RID: 8
		// (Invoke) Token: 0x06000034 RID: 52
		private delegate void screenCursorDelegate(Cursor cursor);

		// Token: 0x02000009 RID: 9
		// (Invoke) Token: 0x06000038 RID: 56
		private delegate void WriteOutputDelegate(string output);
        private Bitmap m_imageSrc = null;
    }
}
